package yanc.app

import grails.converters.JSON
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import java.text.SimpleDateFormat

class FeedbackController {
    def feedbackService
    static final Logger logger = LoggerFactory.getLogger(FeedbackController.class)

    def index() {
        render(view: "feedback")
    }

    def add() {
        def feedback = new Feedback()
        bindData(feedback, params)
        def flag = feedbackService.sa(feedback)
        if (flag) {
            render(view: "feedbackSucc")
        }
    }

    def getAll() {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd")
        def data = [:]
        def page = Integer.parseInt("${params.page ?: 1}")
        def pagesize = Integer.parseInt("${params.pagesize ?: 20}")
        def createDate1 = "${params.createDate1 ?: ''}"
        def createDate2 = "${params.createDate2 ?: ''}"
        def a = Feedback.createCriteria()
        def list = a.list(max: pagesize, offset: (page - 1) * pagesize) {
            and {
                if (!createDate1.isEmpty()) {
                    ge("createDate", new Date(Long.parseLong(createDate1)))
                }
                if (!createDate2.isEmpty()) {
                    def start1 = sdf2.format(new Date(Long.parseLong(createDate2)))
                    Date end1 = sdf1.parse(start1 + " 23:59:59")
                    le("createDate", end1)
                }
            }
            order("createDate","desc")
        }
        def b = Feedback.createCriteria()
        def length = b.get {
            and {
                if (!createDate1.isEmpty()) {
                    ge("createDate", new Date(Long.parseLong(createDate1)))
                }
                if (!createDate2.isEmpty()) {
                    def start1 = sdf2.format(new Date(Long.parseLong(createDate2)))
                    Date end1 = sdf1.parse(start1 + " 23:59:59")
                    le("createDate", end1)
                }
            }
            projections {
                rowCount()
            }
        }
        if (list) {
            data << [statuCode: "0", errMsg: "0", data: list, total: length]
        } else {
            data << [statuCode: "1", errMsg: "1"]
        }
        render data as JSON
    }
}
